package cc.co.cutenimy.nimyexplore.jdbc.metadata;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cc.co.cutenimy.nimyexplore.model.metadata.NResultSetColumn;
import cc.co.cutenimy.nimyexplore.model.metadata.ObjectFactory;

import com.google.inject.Inject;

/**
 * Used to parse general database result set meta data. If there are some
 * specific setting or action for database, please extend this class.
 * 
 * @author 234aini@gmail.com
 * 
 */
public class AutoResultSetMetaDataParser implements ResultSetMetaDataParser {

	@Inject
	public AutoResultSetMetaDataParser(final ResultSetMetaData resultSetMetaData) {
		this.resultSetMetaData = resultSetMetaData;
	}

	private ResultSetMetaData resultSetMetaData;

	@Inject
	private ObjectFactory objectFactory;

	public List<NResultSetColumn> listColumns() throws SQLException {
		ResultSetMetaData meta = getResultSetMetaData();
		List<NResultSetColumn> list = new ArrayList<NResultSetColumn>(
				meta.getColumnCount());
		for (int i = 0; i < meta.getColumnCount(); i++) {
			NResultSetColumn column = getObjectFactory()
					.createNResultSetColumn();
			int index = i + 1;
			column.setAutoIncrement(meta.isAutoIncrement(index));
			column.setCaseSensitive(meta.isCaseSensitive(index));
			column.setCurrency(meta.isCurrency(index));
			column.setDefinitelyWritable(meta.isDefinitelyWritable(index));
			column.setNullable(meta.isNullable(index));
			column.setReadOnly(meta.isReadOnly(index));
			column.setSearchable(meta.isSearchable(index));
			column.setSigned(meta.isSigned(index));
			column.setWritable(meta.isWritable(index));

			column.setColumnClassName(meta.getColumnClassName(index));
			column.setColumnLabel(meta.getColumnLabel(index));
			column.setColumnType(meta.getColumnType(index));
			column.setColumnName(meta.getColumnName(index));
			column.setColumnTypeName(meta.getColumnTypeName(index));
			list.add(column);
		}
		return list;
	}

	public ResultSetMetaData getResultSetMetaData() {
		return resultSetMetaData;
	}

	public ObjectFactory getObjectFactory() {
		return objectFactory;
	}

	public void setObjectFactory(ObjectFactory objectFactory) {
		this.objectFactory = objectFactory;
	}

	public void setResultSetMetaData(ResultSetMetaData resultSetMetaData) {
		this.resultSetMetaData = resultSetMetaData;
	}

}
